<template>
  <section id="contact" class="contact">
    <div class="container">
        <div class="section-header">
          <h2 class="section-title">联系我们</h2>
          <p class="section-subtitle">让我们一起探讨AI技术的无限可能，推动校企合作与人才培养</p>
          <!-- 调试信息 -->
         
        </div>
      <div class="contact-content">
        <div class="contact-info">
          <div class="contact-item">
            <i class="fas fa-map-marker-alt"></i>
            <div>
              <h4>公司地址</h4>
              <p>安徽省芜湖市镜湖区“神山智谷”</p>
            </div>
          </div>
          <div class="contact-item phone-slider-item">
            <div class="phone-slider-wrapper">
              <PhoneSlider />
            </div>
          </div>
          <div class="contact-item">
            <i class="fas fa-envelope"></i>
            <div>
              <h4>邮箱地址</h4>
              <p>starmc@skillith.cn</p>
            </div>
          </div>
        </div>
        <div class="contact-form">
          <!-- 成功消息 -->
          <div v-if="showSuccess" class="alert alert-success">
            <i class="fas fa-check-circle"></i>
            <span>{{ successMessage }}</span>
          </div>
          
          <!-- 错误消息 -->
          <div v-if="showError" class="alert alert-error">
            <i class="fas fa-exclamation-circle"></i>
            <span>{{ errorMessage }}</span>
          </div>

          <form @submit.prevent="submitForm">
            <div class="form-group">
              <input type="text" v-model="form.name" placeholder="您的姓名" required :disabled="isSubmitting">
            </div>
            <div class="form-group">
              <input type="email" v-model="form.email" placeholder="邮箱地址" required :disabled="isSubmitting">
            </div>
            <div class="form-group">
              <input type="tel" v-model="form.phone" placeholder="联系电话" :disabled="isSubmitting">
            </div>
            <div class="form-group">
              <textarea v-model="form.message" placeholder="留言内容" rows="5" required :disabled="isSubmitting"></textarea>
              <div class="char-count" :class="getCharCountClass()">
                {{ form.message.length }}/2000
              </div>
            </div>
            <div class="form-buttons">
              <button type="submit" class="btn btn-primary" :disabled="isSubmitting">
                <i v-if="isSubmitting" class="fas fa-spinner fa-spin"></i>
                <i v-else class="fas fa-paper-plane"></i>
                {{ isSubmitting ? '发送中...' : '发送消息' }}
              </button>
              <button type="button" class="btn btn-clear" @click="clearForm" :disabled="isSubmitting">
                <i class="fas fa-eraser"></i>
                一键清除
              </button>
            </div>
          </form>
        </div>
      </div>
    </div>
  </section>
</template>

<script>
import { API_ENDPOINTS } from '@/config/api'
import PhoneSlider from './PhoneSlider.vue'

export default {
  name: 'ContactSection',
  components: {
    PhoneSlider
  },
  data() {
    return {
      form: {
        name: '',
        email: '',
        phone: '',
        message: ''
      },
      isSubmitting: false,
      showSuccess: false,
      showError: false,
      errorMessage: '',
      successMessage: ''
    }
  },
  mounted() {
    console.log('Contact组件已挂载')
  },
  methods: {
    async submitForm() {
      // 验证表单
      if (!this.validateForm()) {
        return
      }

      this.isSubmitting = true
      this.hideMessages()

      try {
        console.log('发送请求到:', API_ENDPOINTS.CONTACT)
        console.log('请求数据:', this.form)
        
        let response
        let result
        
        try {
          // 首先尝试HTTPS
          response = await fetch(API_ENDPOINTS.CONTACT, {
            method: 'POST',
            headers: {
              'Content-Type': 'application/json',
            },
            body: JSON.stringify(this.form)
          })
          
          console.log('HTTPS响应状态:', response.status)
          result = await response.json()
          console.log('HTTPS API响应:', result)
          
        } catch (httpsError) {
          console.warn('HTTPS请求失败，尝试HTTP回退:', httpsError.message)
          
          // 如果HTTPS失败，尝试HTTP回退
          if (API_ENDPOINTS.CONTACT_FALLBACK) {
            console.log('尝试回退到:', API_ENDPOINTS.CONTACT_FALLBACK)
            response = await fetch(API_ENDPOINTS.CONTACT_FALLBACK, {
              method: 'POST',
              headers: {
                'Content-Type': 'application/json',
              },
              body: JSON.stringify(this.form)
            })
            
            console.log('HTTP响应状态:', response.status)
            result = await response.json()
            console.log('HTTP API响应:', result)
          } else {
            throw httpsError
          }
        }

        if (result.success) {
          this.showSuccessMessage(result.message || '消息发送成功！我们会尽快与您联系。')
          this.clearForm()
        } else {
          this.showErrorMessage(result.error || '发送失败，请重试')
        }
      } catch (error) {
        console.error('提交错误:', error)
        this.showErrorMessage('网络错误，请检查网络连接后重试')
      } finally {
        this.isSubmitting = false
      }
    },

    validateForm() {
      if (!this.form.name.trim()) {
        this.showErrorMessage('请输入您的姓名')
        return false
      }

      if (!this.form.email.trim()) {
        this.showErrorMessage('请输入您的邮箱')
        return false
      }

      if (!this.isValidEmail(this.form.email)) {
        this.showErrorMessage('请输入有效的邮箱地址')
        return false
      }

      if (!this.form.message.trim()) {
        this.showErrorMessage('请输入消息内容')
        return false
      }

      if (this.form.message.length > 2000) {
        this.showErrorMessage('消息内容不能超过2000个字符')
        return false
      }

      return true
    },

    isValidEmail(email) {
      const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
      return emailRegex.test(email)
    },

    showSuccessMessage(message) {
      console.log('显示成功消息:', message) // 调试信息
      this.successMessage = message
      this.showSuccess = true
      this.showError = false
      
      // 3秒后自动隐藏
      setTimeout(() => {
        this.hideMessages()
      }, 3000)
    },

    showErrorMessage(message) {
      this.errorMessage = message
      this.showError = true
      this.showSuccess = false
    },

    hideMessages() {
      this.showSuccess = false
      this.showError = false
    },

    clearForm() {
      this.form = {
        name: '',
        email: '',
        phone: '',
        message: ''
      }
      this.hideMessages()
    },

    getCharCountClass() {
      const length = this.form.message.length
      if (length > 2000) {
        return 'error'
      } else if (length > 1800) {
        return 'warning'
      }
      return ''
    }
  }
}
</script>

<style scoped>
.contact {
  background: rgba(255, 255, 255, 0.02);
  padding: 120px 0;
  border: 1px solid rgba(255, 255, 255, 0.05);
  margin: 40px 20px;
  border-radius: 24px;
  min-height: 400px; /* 确保有足够的高度 */
  position: relative; /* 确保定位正确 */
  z-index: 1; /* 确保在其他元素之上 */
  overflow: visible; /* 确保子元素可以超出边界 */
}

.contact::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  backdrop-filter: blur(20px);
  border-radius: 24px;
  z-index: -1; /* 将遮罩放在背景层 */
  pointer-events: none; /* 确保不影响交互 */
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.section-header {
  text-align: center;
  margin-bottom: 4rem;
}

.section-title {
  font-size: 3rem;
  font-weight: 600;
  margin-bottom: 1.5rem;
  background: linear-gradient(135deg, #3b82f6, #8b5cf6, #06b6d4);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  position: relative;
  letter-spacing: -0.02em;
  line-height: 1.2;
}

.section-subtitle {
  font-size: 1.2rem;
  color: white;
  max-width: 600px;
  margin: 0 auto;
  line-height: 1.6;
}

.contact-content {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4rem;
}

.contact-info {
  display: flex;
  flex-direction: column;
  gap: 2rem;
}

.contact-item {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 1.5rem;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 15px;
  box-shadow: 0 4px 20px rgba(59, 130, 246, 0.2);
  border: 1px solid rgba(59, 130, 246, 0.2);
  backdrop-filter: blur(10px);
  transition: all 0.3s ease;
}

.contact-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 30px rgba(59, 130, 246, 0.3);
  border-color: rgba(59, 130, 246, 0.4);
}

.contact-item i {
  font-size: 1.5rem;
  color: #3b82f6;
  width: 40px;
  text-align: center;
  text-shadow: 0 0 10px rgba(59, 130, 246, 0.5);
}

.contact-item h4 {
  margin-bottom: 0.5rem;
  color: white;
  font-weight: 600;
}

.contact-item p {
  color: white;
  margin: 0;
}

.phone-slider-item {
  flex-direction: column;
  align-items: stretch;
  padding: 0;
  background: transparent;
  box-shadow: none;
  border: none;
  backdrop-filter: none;
}

.phone-slider-item:hover {
  transform: none;
  box-shadow: none;
  border-color: transparent;
}

.phone-slider-wrapper {
  width: 100%;
  padding: 1rem;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 15px;
  box-shadow: 0 4px 20px rgba(59, 130, 246, 0.2);
  border: 1px solid rgba(59, 130, 246, 0.2);
  backdrop-filter: blur(10px);
  transition: all 0.3s ease;
}

.phone-slider-wrapper:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 30px rgba(59, 130, 246, 0.3);
  border-color: rgba(59, 130, 246, 0.4);
}

.contact-form {
  background: rgba(255, 255, 255, 0.05);
  padding: 2.5rem;
  border-radius: 20px;
  box-shadow: 0 4px 20px rgba(59, 130, 246, 0.2);
  border: 1px solid rgba(59, 130, 246, 0.2);
  backdrop-filter: blur(10px);
}

.form-group {
  margin-bottom: 1.5rem;
}

.form-group input,
.form-group textarea {
  width: 100%;
  padding: 12px 16px;
  border: 2px solid rgba(59, 130, 246, 0.3);
  border-radius: 12px;
  font-size: 1rem;
  transition: all 0.3s ease;
  background: rgba(255, 255, 255, 0.05);
  color: white;
  backdrop-filter: blur(10px);
}

.form-group input::placeholder,
.form-group textarea::placeholder {
  color: rgba(255, 255, 255, 0.8);
}

.form-group input:focus,
.form-group textarea:focus {
  outline: none;
  border-color: #3b82f6;
  box-shadow: 0 0 20px rgba(59, 130, 246, 0.3);
  background: rgba(255, 255, 255, 0.08);
}

.form-group textarea {
  resize: vertical;
  min-height: 340px;
}

.btn {
  padding: 12px 30px;
  border: none;
  border-radius: 50px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  text-decoration: none;
}

.form-buttons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  width: 100%;
}

.btn-primary {
  background: linear-gradient(135deg, #00d4ff, #3b82f6);
  color: white;
  box-shadow: 0 4px 15px rgba(0, 212, 255, 0.4);
  border: 1px solid rgba(0, 212, 255, 0.3);
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(0, 212, 255, 0.6);
  background: linear-gradient(135deg, #00b8e6, #2563eb);
}

.btn-clear {
  background: rgba(255, 255, 255, 0.05);
  color: rgba(255, 255, 255, 0.8);
  border: 2px solid rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  transition: all 0.3s ease;
}

.btn-clear:hover {
  transform: translateY(-2px);
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.4);
  box-shadow: 0 4px 15px rgba(255, 255, 255, 0.2);
  color: white;
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none !important;
}

.btn:disabled:hover {
  transform: none !important;
  box-shadow: none !important;
}

/* 消息提示样式 */
.alert {
  padding: 1rem 1.5rem;
  border-radius: 12px;
  margin-bottom: 1.5rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 500;
  animation: slideDown 0.3s ease;
}

.alert-success {
  background: rgba(34, 197, 94, 0.1);
  border: 1px solid rgba(34, 197, 94, 0.3);
  color: #22c55e;
  backdrop-filter: blur(10px);
}

.alert-error {
  background: rgba(239, 68, 68, 0.1);
  border: 1px solid rgba(239, 68, 68, 0.3);
  color: #ef4444;
  backdrop-filter: blur(10px);
}

.alert i {
  font-size: 1.1rem;
}

/* 字符计数 */
.char-count {
  text-align: right;
  font-size: 0.8rem;
  color: rgba(255, 255, 255, 0.6);
  margin-top: 0.5rem;
}

.char-count.warning {
  color: #f59e0b;
}

.char-count.error {
  color: #ef4444;
}

/* 加载动画 */
.fa-spinner {
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 校企合作样式 */
.cooperation {
  margin-top: 4rem;
  padding-top: 3rem;
  border-top: 2px solid rgba(59, 130, 246, 0.2);
}

.cooperation-title {
  text-align: center;
  font-size: 2rem;
  margin-bottom: 2rem;
  color: white;
  font-weight: 600;
}

.cooperation-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 2rem;
}

.cooperation-card {
  background: rgba(255, 255, 255, 0.05);
  padding: 2rem;
  border-radius: 20px;
  box-shadow: 0 4px 20px rgba(59, 130, 246, 0.2);
  border: 1px solid rgba(59, 130, 246, 0.2);
  text-align: center;
  transition: all 0.3s ease;
  backdrop-filter: blur(10px);
}

.cooperation-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 30px rgba(59, 130, 246, 0.3);
  border-color: rgba(59, 130, 246, 0.4);
}

.cooperation-icon {
  width: 60px;
  height: 60px;
  background: linear-gradient(135deg, #3b82f6, #8b5cf6);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 1rem;
  box-shadow: 0 0 20px rgba(59, 130, 246, 0.5);
}

.cooperation-icon i {
  font-size: 1.5rem;
  color: white;
}

.cooperation-card h4 {
  font-size: 1.2rem;
  margin-bottom: 1rem;
  color: white;
  font-weight: 600;
}

.cooperation-card p {
  color: white;
  line-height: 1.6;
}

@media (max-width: 768px) {
  .contact-content {
    grid-template-columns: 1fr;
  }
  
  .form-buttons {
    grid-template-columns: 1fr 1fr;
    gap: 0.8rem;
  }
  
  .btn {
    width: 100%;
    min-width: 0;
  }
}

@media (max-width: 480px) {
  .form-buttons {
    grid-template-columns: 1fr;
    gap: 0.5rem;
  }
}
</style>
